package com.itheima.sc.service.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.order.OmsOrderApi;
import com.itheima.sc.pojo.order.OmsOrder;
import com.itheima.sc.pojo.order.OmsOrderItem;
import com.itheima.sc.pojo.order.OmsOrderOperateHistory;
import com.itheima.sc.result.Result;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OmsOrderService {

    @DubboReference
    OmsOrderApi omsOrderApi;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    OmsOrderUtil omsOrderUtil;


    public Result listOmsOrder(Integer page, Integer size, Map<String, String> map) {
        //订单列表
        Page<OmsOrder> omsOrders = omsOrderApi.listOmsOrder(page, size, map);
        PageResult pageResult = new PageResult(page, size, Convert.toInt(omsOrders.getTotal()),Convert.toInt(omsOrders.getPages()), omsOrders.getRecords());
        return Result.success(pageResult);
    }

    public Result deletByIds(List<Long> ids) {
        if (ObjectUtil.isEmpty(ids) || ids.size() == 0) {
            return Result.error("请先选择订单");
        }
        List<Integer> orderStatusByIds = omsOrderApi.getOrderStatusByIds(ids);
        if (orderStatusByIds.contains(0)||orderStatusByIds.contains(1)||orderStatusByIds.contains(2)){
            return Result.error("请先完成订单");
        }
        omsOrderApi.deletByIds(ids);
        return Result.success("删除订单成功");
    }
//如果是待发货，修改退款金额。
    public Result closeOrderByIds(ArrayList<Long> ids,String note) {
        if (ObjectUtil.isEmpty(ids) || ids.size() == 0) {
            return Result.error("请先选择要关闭的订单");
        }
        //已关闭订单和无效订单不能关闭
        List<Integer> orderStatusByIds = omsOrderApi.getOrderStatusByIds(ids);
        if (orderStatusByIds.contains(4)||orderStatusByIds.contains(5)||orderStatusByIds.contains(1)||orderStatusByIds.contains(2)){
            return Result.error("当前选项不是全为待付款或已完成订单");
        }
        Boolean flage = omsOrderApi.updateStatusByIds(ids, 4,note);//关闭订单
        //保存操作历史记录
        omsOrderUtil.saveHistoryOpe(ids,4,"关闭订单");

        return Result.success("关闭成功");
    }


    //批量发货1待发，2已发
    public Result deliveryOrderByIds(Long id, Map<String, String> map) {
        if (ObjectUtil.isEmpty(id)) {
            return Result.error("请先选择要发货的订单");
        }

        OmsOrder orderById = omsOrderApi.getOrderById(id);

        if (orderById.getStatus()!=1){
            return Result.error("您当前选择的不是所有代发订单");
        }
        //发货，修改配送公司，快递单号，发货时间
        Boolean flage = omsOrderApi.delivery(map);//批量发货
        //保存历史记录
        omsOrderUtil.saveHistoryOpe(CollUtil.toList(id),2,"完成发货");
        return Result.success("发货成功");
    }

    public Result updateMoneyInfo(Map<String, String> moneyInfoParam) {
        //修改订单的价格
        if (ObjectUtil.isEmpty(moneyInfoParam)) {
            return Result.error("请选择操作");
        }
        omsOrderApi.updateMoneyInfo(moneyInfoParam);
        return Result.success("修改成功");
    }


    public ResponseEntity noteById(Map<String, String> map) {
        //修改订单的备注
        if (ObjectUtil.isEmpty(map)) {
            return ResponseEntity.ok("备注为空");
        }
        omsOrderApi.updateNoteById(map);

        return ResponseEntity.ok("修改成功");
    }

    public Result receiverInfoById(Map<String, String> map) {
        //订单状态要小于2
        Result boolStatusResult = boolStatus(map, 2, "请先下单");
        if (boolStatusResult != null) return boolStatusResult;
        //修改收货人信息
        omsOrderApi.updateReceiverInfoById(map);
        return Result.success("修改成功");
    }

    private Result boolStatus(Map<String, String> map, Integer status, String message) {
        if (Convert.toInt(map.get("status")) >= status) {
            return Result.error(message);
        }
        return null;
    }



    public OmsOrder orderInfoById(Long id) {
        OmsOrder omsOrder = omsOrderApi.getOrderById(id);
        List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderApi.getomsOrderOperateHistories(id);
        List<OmsOrderItem> omsOrderItems = omsOrderApi.getOmsOrderItem(id);
        omsOrder.setOrderItemList(omsOrderItems);
        omsOrder.setHistoryList(omsOrderOperateHistories);
        return omsOrder;
    }



}
